Skip to content

Conversation

@susmonteiro
Copy link
Contributor

The annotationOnly flag was originally introduced to control for which types we would infer escapability, and for which we would rely solely on annotations. However, due to backward compatibility issues, this flag is currently always set to true. Therefore, this patch removes the annotationOnly flag and makes the compiler infer escapability from bases and fields only for simple records, such as aggregates.

@susmonteiro susmonteiro added the c++ interop Feature: Interoperability with C++ label Nov 21, 2025
@susmonteiro susmonteiro changed the title Susmonteiro/remove annotationonly flag [cxx-interop] Remove annotationOnly flag from Escapability request Nov 21, 2025
@susmonteiro
Copy link
Contributor Author

Note: this PR is a follow-up to #85485, which hasn't been merged yet. Only the last commit 6279d81 is a new addition

@susmonteiro
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@j-hui j-hui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed only the changes in 6279d81 and am fine with them as is, but I'm wondering what the plan is for having this analysis actually be accurate and consider structs with pointer fields as non-escaping.

type->isMemberPointerType() || type->isReferenceType()) {
// pointer and reference types are currently imported as unknown
// (importing them as non-escapable broke backward compatibility)
hasUnknown = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this for now, to preserve the current behavior, but assuming pointers have unknown escapability seems like it could be problematic. My understanding is that, semantically, pointers should be treated as non-escapable.

Do we have a plan for how to correctly analyze these types while maintaining backwards compat?

An idea I had was making the analysis depend on whether strict memory safety is turned on or off, essentially using/abusing it as a feature flag. But that may not be a good idea for reasons that I haven't fully thought through yet.

void someMethod() {}
};

// This is a complex record, so we don't infer escapability.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave a reminder about what makes this record complex? Is it the presence of a user-defined default constructor? Or copy constructor?

View b;
bool c;

ComplexRecord() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please initialize the fields

// as such
// - a record type is escapable if it is annotated with SWIFT_ESCAPABLE_IF()
// and none of the annotation arguments are non-escapable
// - an aggregate or a record that is not a cxxrecord are escapable if none of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: simplify wording

Suggested change
// - an aggregate or a record that is not a cxxrecord are escapable if none of
// - an aggregate or non-cxx record is escapable if none of

I think it might also be helpful to spell out what it means to be an "aggregate" record. In my mind I read this as basically being "a C-like record", i.e., not having user-defined behavior for copy construction, but maybe that's inaccurate/incomplete.

Also, can you look up/document the reason why we are distinguish aggregate records from non-aggregate/"complex" records?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see that the reasoning is sort of written about in a later comment. I think that should be brought up here, to keep things in one place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ interop Feature: Interoperability with C++

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants